package com.imooc.coupon.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

/**
 * @description:
 * @author：naxiaozi，微信：naxiaozi
 * @date: 2025/8/3
 * @Copyright：
 */
//通用的抽象过滤器类
public abstract class AbstractZuulFilter extends ZuulFilter {

    RequestContext context;
    private final static String NEXT="next";
    @Override
    public boolean shouldFilter() {
        RequestContext ctxt = RequestContext.getCurrentContext();
        return (boolean) ctxt.getOrDefault(NEXT,true);
    }

    @Override
    public Object run() throws ZuulException {
        context = RequestContext.getCurrentContext();
        return cRun();
    }
    protected abstract Object cRun();

    Object fail(int code,String msg){
        context.set(NEXT,false);
        context.setSendZuulResponse(false);
        context.getResponse().setContentType("text/html;charset=UTF-8");
        context.setResponseStatusCode(code);
        context.setResponseBody(String.format(
                "{\"result\":\"%s!\"}",msg
        ));
        return null;
    }
    Object success(){
        context.set(NEXT,true);
        return null;
    }
}
